import { DataService } from './data.service';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { RestService } from './rest.service';

@Injectable()
export class LoginService {
  user: any;

  constructor(public router: Router,
              public restService: RestService,
              public dataService: DataService) {
    this.user = this.restService.getUser();
  }

  public reload2Login() {
    this.router.navigateByUrl('/');
    location.reload();
  }

  public logOut(): void {
    this.restService.post('logout', this.user).subscribe(response => {
      this.restService.cleanupLocalStorage();
      this.reload2Login();
    }, error => {
      this.restService.cleanupLocalStorage();
      this.reload2Login();
    });
  }
}
